package com.wuyan.locationdog.ui.my


import android.os.Bundle
import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.VersionContentBean
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_info_modify
import com.arcsoft.commonbase.bus.bus_update_userinfo
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.loadImageCircle
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.net.ApiService
import com.arcsoft.commonbase.utils.clearCache
import com.arcsoft.commonbase.utils.getCacheSize
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.azhon.appupdate.manager.DownloadManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.databinding.SetActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.ui.X5WebActivity
import com.wuyan.locationdog.ui.login.RegisterActivity
import com.wuyan.locationdog.viewmodel.MyViewModel

class SetActivity : BaseBindingActivity<SetActivityBinding>() {
    private val mViewModel: MyViewModel by viewModels()

    override fun initData() {
        setTitleText("设置")

        binding.apply {
            if (LoginHelp.isDeviceLogin) {
                btnPhoto.gone()
                btnNickName.gone()
                btnPhone.gone()
                btnBindPhone.visible()
                DeviceHelp.selectDeviceBeanContent.apply {
                    if (!deviceDetailVO.userPhone.isNullOrEmpty()) {
                        phone.text = deviceDetailVO.userPhone
                        phone.setTextColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.color_3))
                        bindPhoneArrow.gone()
                    } else {
                        btnBindPhone.setOnClickListener {
                            val bundle = Bundle()
                            bundle.putInt("type", 8)
                            bundle.putBoolean("isBindPhone", true)
                            ActivityUtils.startActivity(bundle, RegisterActivity::class.java)
                        }
                    }
                }

                btnModifyPwd.setOnClickListener {
                    DeviceHelp.isDeviceModifyPwd = true
                    ActivityUtils.startActivity(MyModifyPwdActivity::class.java)
                }
            } else {
                btnModifyPwd.setOnClickListener {
                    DeviceHelp.isDeviceModifyPwd = false
                    ActivityUtils.startActivity(MyModifyPwdActivity::class.java)
                }
            }

            tvVersion.text = AppUtils.getAppVersionName()

            //缓存清理
            cache.text = getCacheSize(mActivity)
            btnClear.setOnClickListener {
                CommonDialog(mActivity, "清理缓存", "清理缓存后不会丢失您的设备数据和个人信息。") {
                    clearCache(mActivity)
                    cache.text = "0kb"
                }.show()
            }

            btnSure.setOnClickListener {
                CommonDialog(mActivity, "确定退出登录吗？", "退出登录将无法查看数据") {
                    LoginHelp.logout()
                }.show()
            }


            btnVersion.setOnClickListener {
                mViewModel.getVersion(hashMapOf())
            }
            btnAgree.setOnClickListener {
                X5WebActivity.startAction(mActivity, ApiService.BASE_URL + "html/privacyAgreement.html")
            }


            btnPhoto.setOnClickListener {
                ActivityUtils.startActivity(MyPhotoActivity::class.java)
            }
            btnNickName.setOnClickListener {
                ActivityUtils.startActivity(MyNickNameActivity::class.java)
            }
            btnPhone.setOnClickListener {
                ActivityUtils.startActivity(MyModifyPhoneActivity::class.java)
            }

        }
    }

    override fun loadNetData() {
        super.loadNetData()
        mViewModel.getMyInfo(hashMapOf())
    }

    override fun observe() {
        super.observe()
        Bus.observe<String>(bus_update_userinfo, this) {
            loadNetData()
        }
        Bus.observe<String>(bus_device_info_modify, this) {
            loadNetData()
        }
        mViewModel.myInfoBean.observe(this) {
            binding.apply {
                ivHead.loadImageCircle(it.content.avatarDTO.fileUrl)
                tvNickName.setText(it.content.nickName)

                if (LoginHelp.isDeviceLogin) {
                    btnPhoto.gone()
                    btnNickName.gone()
                    btnPhone.gone()
                    btnBindPhone.visible()
                    DeviceHelp.selectDeviceBeanContent.deviceDetailVO.userPhone = it.content.userPhone
                    DeviceHelp.selectDeviceBeanContent.apply {
                        if (!deviceDetailVO.userPhone.isNullOrEmpty()) {
                            phone.text = deviceDetailVO.userPhone
                            phone.setTextColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.color_3))
                            bindPhoneArrow.gone()
                        } else {
                            btnBindPhone.setOnClickListener {
                                val bundle = Bundle()
                                bundle.putInt("type", 8)
                                bundle.putBoolean("isBindPhone", true)
                                ActivityUtils.startActivity(bundle, RegisterActivity::class.java)
                            }
                        }
                    }

                    btnModifyPwd.setOnClickListener {
                        DeviceHelp.isDeviceModifyPwd = true
                        ActivityUtils.startActivity(MyModifyPwdActivity::class.java)
                    }
                }
            }

        }
        mViewModel.versionBean.observe(this) {
            if (it.content.appVersionCode > AppUtils.getAppVersionCode()) {
                downLoad(it.content)
            } else {
                customShowLong("当前已是最新版本")
            }

        }
    }

    private fun downLoad(it: VersionContentBean) {
        val manager = DownloadManager.Builder(ActivityUtils.getTopActivity()).run {
            apkUrl(it.url)
            apkName(AppUtils.getAppName() + ".apk")
            smallIcon(R.mipmap.launcher_logo)
            //设置了此参数，那么内部会自动判断是否需要显示更新对话框，否则需要自己判断是否需要更新
            apkVersionCode(it.appVersionCode)
            //同时下面三个参数也必须要设置
            apkVersionName(it.appVersionName)
//            apkTitle(it.updateAppTitle)
//            apkSize(it.updateAppSize)
            apkDescription(it.appDesc)
            //省略一些非必须参数...
//            apkMD5(it.updateAppMd5)
//            forcedUpgrade(true)
//            jumpInstallPage(it.jumpInstallPage)
//            dialogButtonColor(Color.WHITE)
//            dialogButtonTextColor(Color.WHITE)
            dialogProgressBarColor(ColorUtils.getColor(com.arcsoft.commonbase.R.color.text_color_selected))
            build()
        }

        manager.download()
    }

}